#include "gamescene.h"
#include<QDebug>
#include<QMenuBar>
#include<qpainter.h>
#include<QPixmap>
#include <QMessageBox>
const int TARGET_WIDTH=100;
gamescene::gamescene(QWidget *parent)
    : QMainWindow{parent}
{
    this->setFixedSize(1211,800);
    this->setWindowTitle("不能死啊啊啊");
    QMenuBar *bar=menuBar();//创建菜单栏
    setMenuBar(bar);
    QMenu*startmenu=bar->addMenu("设置");
    QAction *quitaction=startmenu->addAction("退出");
    QAction *restartaction=startmenu->addAction("重新开始");
     QAction *pauseaction = startmenu->addAction("暂停");
    QAction *continueaction=startmenu->addAction("继续");
    connect(quitaction,&QAction::triggered,[=](){
        this->close();
    });
    connect(restartaction,&QAction::triggered,[=](){
        this->close();
        gamescene*game=new gamescene;
        game->show();
       // game->setAttribute(Qt::WA_DeleteOnClose);
       // game->resetGame(); // 重置游戏状态
        //game->startmoving();
    });
    connect(pauseaction, &QAction::triggered, [=]() {
        if (!isPaused) {
            isPaused = true;
            anitimer->stop(); // 停止动画定时器
            moveTimer->stop(); // 停止移动定时器
            qDebug() << "Game paused.";
        }
    });
    connect(continueaction, &QAction::triggered, [=]() {
        if (isPaused) { // 假设 isPaused 是一个标志变量，表示游戏是否暂停
            isPaused = false;
            // 恢复游戏逻辑，例如重新启动定时器
            anitimer->start(); // 恢复动画定时器
            moveTimer->start(); // 恢复移动定时器
        }
    });
//返回按键
    // 创建用于显示小人的 QLabel
    runningManLabel = new QLabel(this);
    runningManLabel->move(200, 400); // 设置小人显示的指定位置
    // 初始化动画定时器
    anitimer = new QTimer(this);
    connect(anitimer, &QTimer::timeout, this, &gamescene::updateRunningAnimation);
    anitimer->start(100);
    qDebug() << "Animation timer started!";// 每 100 毫秒更新一次动画
    // 初始化帧索引
    currentFrame = 0;
    // 创建用于显示移动物体的QLabel
    movingObjectLabel = new QLabel(this);
    QPixmap objectPixmap(":/picture/obstacle.jpg");// 假设物体图片为 object.png
    objectPixmap = objectPixmap.scaled(100, 110, Qt::KeepAspectRatio);
    movingObjectLabel->setPixmap(objectPixmap);
    movingObjectLabel->adjustSize();
    objectX = 800;
    objectY = 400;
    movingObjectLabel->move(objectX, objectY);

    // 初始化物体移动定时器
    moveTimer = new QTimer(this);
    connect(moveTimer, &QTimer::timeout, this, &gamescene::moveObject);
    //moveTimer->start(10); // 每20毫秒移动一次

    // 初始化碰撞检测定时器
    connect(moveTimer, &QTimer::timeout, this, &gamescene::checkCollision);


    collisionDetected = false;
}

/*gamescene::~gamescene()
{
 delete anitimer;
    delete moveTimer;
    delete runningManLabel;
    delete movingObjectLabel;
}*/

void gamescene:: paintEvent(QPaintEvent*){
    QPainter painter(this);
    QPixmap pix;
    pix.load("://picture/gamebackground.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);//拉伸成窗口大小
}
void gamescene::updateRunningAnimation()
{  qDebug() << "updateRunningAnimation called!";
    if (collisionDetected || isPaused) {
        return; // 碰撞发生后停止小人动画
    }

    // 构建当前帧的图片路径
    QString imagePath = QString(":/picture/%1.jpg").arg(currentFrame);
    QPixmap pixmap(imagePath);
    if (!pixmap.isNull()) {
        // 计算图片的宽高比
        double aspectRatio = (double)pixmap.height() / pixmap.width();
        // 根据目标宽度和宽高比计算目标高度
        int targetHeight = static_cast<int>(TARGET_WIDTH * aspectRatio);

        // 等比例缩放图片
        pixmap = pixmap.scaled(TARGET_WIDTH, targetHeight, Qt::KeepAspectRatio);
        // 显示图片
        runningManLabel->setPixmap(pixmap);
        runningManLabel->setFixedSize(TARGET_WIDTH, targetHeight);
         qDebug() << "Image loaded successfully:" << imagePath;
    } else {
        qDebug() << "Failed to load image:" << imagePath;
    }


    // 更新帧索引
    currentFrame = (currentFrame + 1) % frameCount;
    qDebug() << "Current frame:" << currentFrame;
    // 处理跳跃逻辑
    if (isJumping) {
        if (jumpHeight < maxJumpHeight) {
            runningManLabel->move(runningManLabel->x(), runningManLabel->y() - jumpStep); // 向上移动
            jumpHeight += jumpStep;
            qDebug() << "Jumping - Height:" << jumpHeight << "Position:" << runningManLabel->x() << runningManLabel->y();
        } else {
            isJumping = false;
            //jumpHeight = 0; // 重置跳跃高度
            qDebug() << "Jump finished!";
        }
    } else if (jumpHeight > 0) {
        runningManLabel->move(runningManLabel->x(), runningManLabel->y() + jumpStep); // 向下移动
        jumpHeight -= jumpStep;
        qDebug() << "Falling - Height:" << jumpHeight << "Position:" << runningManLabel->x() << runningManLabel->y();
    }
}
void gamescene::moveObject()
{
    if (collisionDetected||isPaused) {
        return; // 碰撞发生后停止物体移动
    }

    if (objectX > targetX ) {
        objectX -= 1;
    }
    if (objectY > targetY) {
        objectY -= 1;
    }

    movingObjectLabel->move(objectX, objectY);
    if (objectX <= 200) {
        movingObjectLabel->hide(); // 隐藏物体
        movingObjectLabel->deleteLater(); // 销毁物体
    }
}
void gamescene::checkCollision()
{
    if (collisionDetected) {
        return;
    }

    QRect manRect = runningManLabel->geometry();
    QRect objectRect = movingObjectLabel->geometry();

    if (manRect.intersects(objectRect)) {
        collisionDetected = true;
        // 触发特定事件，这里简单打印日志
        anitimer->stop();
        moveTimer->stop();

        // 弹出游戏失败提示窗口
        QMessageBox::critical(this, "游戏失败", "碰撞发生，游戏结束！");
        // 你可以在这里添加更多的触发逻辑，比如播放音效、显示提示框等
    }
}
void gamescene::startmoving()
{
    ismoving = true; // 启动物体移动
    moveTimer->start(10); // 每 50ms 移动一次
}
void gamescene::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "Mouse button clicked:" << event->button();
    if (event->button() == Qt::LeftButton) {
        qDebug() << "Left mouse button clicked!";
        if (!isJumping) {
            isJumping = true; // 开始跳跃
        }
    }
}
/*void gamescene::resetGame()
{
    // 重置物体位置
    objectX = 800;
    objectY = 400;
    movingObjectLabel->move(objectX, objectY);

    // 重置碰撞状态
    collisionDetected = false;

    // 重置暂停状态
    isPaused = false;
isJumping=false;
JumpHeight=0;
    // 启动定时器
    anitimer->start();
    moveTimer->start();
}*/
